package com.zyl.edthresholdtraining.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zyl.edthresholdtraining.entity.TimePartitionAllow;
import com.zyl.edthresholdtraining.mapper.TimePartitionAllowMapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.List;

/**
 * @author jiangtingfeng
 * @description
 * @date 2020/8/1/001
 */
@Slf4j
@Service
public class TimePartitionAllowService extends ServiceImpl<TimePartitionAllowMapper, TimePartitionAllow> {

    public TimePartitionAllow getTimePartitionAllowService(String userId, String currentPartition,String currentDate) {
        QueryWrapper<TimePartitionAllow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id",userId).eq("time_partition",currentPartition).eq("date_mark",currentDate);
        List<TimePartitionAllow> timePartitionAllowList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(timePartitionAllowList)) {
            return null;
        }
        return timePartitionAllowList.get(0);
    }

    public String getDateMark(String userId) {
        QueryWrapper<TimePartitionAllow> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("user_id", userId).orderByDesc("create_time");
        List<TimePartitionAllow> timePartitionAllowList = baseMapper.selectList(queryWrapper);
        if (CollectionUtils.isEmpty(timePartitionAllowList)) {
            return "";
        } else {
            TimePartitionAllow timePartitionAllow = timePartitionAllowList.get(0);
            return timePartitionAllow.getDateMark();
        }
    }
}
